package com.ymatou.iapi.app.keyword.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.app.keyword.parameter.SellerBean;
import com.ymatou.iapi.app.keyword.service.SellerCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P3;
import com.ymttest.database.sqlwapper.SearchWapper;

public class Ts_Seller {
	private static SellerBean sellerBean;
	private static SellerCall sellerCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("app店铺搜索接口");
	}

	@Before
	public void caseUp() {
		sellerBean=new SellerBean();
		sellerCall=new SellerCall();
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sellerCall.getOpurl());
		
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Seller_001() {
		Logger.start(true,"店铺搜索- 验证接口模糊匹配返回店铺信息,接口能正常返回店主名和id");
		try {
			//设置参数
			String keyword = "奶粉";
			String vagueName = "nickname"; //模糊匹配
			String originName = "originnickname";//精确匹配
			String requestid = "53453gge4er3freg";
			
			sellerBean.setModule("seller");
			sellerBean.setAppkey(1);
			sellerBean.setQuery("keyword("+vagueName+","+keyword+")");
			sellerBean.setPlatform("pc");
			sellerBean.setExplain("Flase");
			sellerBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,sort:apphaohuo1");
			sellerBean.setFl("iuserid,shopname,nickname,originnickname,ismerchant");
			sellerBean.setLimit("0,10");
			sellerBean.setRequestid(requestid);
			
			sellerCall.setData(sellerBean);
			sellerCall.callService();	
			
			JSONArray hits = sellerCall.getHits();
			
			Logger.verifyEquals("OK", sellerCall.getStatus(), "接口返回状态值正常");
			
			for (int i = 1; i <= hits.length(); i++) {
				String hitsOne = hits.getString(i - 1);
				JSONObject hitsOnes = new JSONObject(hitsOne);
				
				Logger.verifyNotNull(hitsOnes.get("originnickname"), "originnickname商户名不能为空");
				Logger.verifyNotNull(hitsOnes.get("iuserid"), "iuserid商户id不能为空");
				
			}
			
				
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Seller_002() {
		Logger.start(true,"店铺搜索- 验证接口模糊匹配返回的店铺是否重复");
		try {
			//设置参数
			String keyword = "奶粉";
			String vagueName = "nickname"; //模糊匹配
			String requestid = "53453gge4er3freg";
			
			sellerBean.setModule("seller");
			sellerBean.setAppkey(1);
			sellerBean.setQuery("keyword("+vagueName+","+keyword+")");
			sellerBean.setPlatform("pc");
			sellerBean.setExplain("Flase");
			sellerBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,sort:apphaohuo1");
			sellerBean.setFl("iuserid,shopname,nickname,originnickname,ismerchant");
			sellerBean.setLimit("0,10");
			sellerBean.setRequestid(requestid);
			
			sellerCall.setData(sellerBean);
			sellerCall.callService();	
			
			JSONArray hitsAry = sellerCall.getHits();

			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {

				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);

				String id = json.getString("iuserid");

				if (list.contains(id) == true) {

					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的id存在重复值" + id);

				} else {
					Logger.verifyEquals(false, list.contains(id), "验证第" + i+ "条返回数据的id不存在重复值" + id);
					list.add(id);
				}

			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Seller_003() {
		Logger.start(true,"店铺搜索- 验证接口模糊匹配返回店铺信息是否匹配关键字");
		try {
			//设置参数
			String keyword = "奶粉";
			String vagueName = "nickname"; //模糊匹配
			String requestid = "53453gge4er3freg";
			
			sellerBean.setModule("seller");
			sellerBean.setAppkey(1);
			sellerBean.setQuery("keyword("+vagueName+","+keyword+")");
			sellerBean.setPlatform("pc");
			sellerBean.setExplain("Flase");
			sellerBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,sort:apphaohuo1");
			sellerBean.setFl("iuserid,shopname,nickname,originnickname,ismerchant");
			sellerBean.setLimit("0,10");
			sellerBean.setRequestid(requestid);
			
			sellerCall.setData(sellerBean);
			sellerCall.callService();	
			
			JSONArray hits = sellerCall.getHits();
			
			for (int i = 1; i <= hits.length(); i++) {
				String hitsOne = hits.getString(i - 1);
				JSONObject hitsOnes = new JSONObject(hitsOne);
				
				Logger.verifyIsContains(keyword, hitsOnes.get("originnickname"), "检查第"+i+"条数据是否匹配关键字"+keyword);
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Seller_004() {
		Logger.start(true,"店铺搜索- 验证接口精确匹配店铺信息，只会返回1条精准记录");
		try {
			//设置参数
			String keyword = "德国奶粉代购";// 对应的userid=1516988
			String originName = "originnickname";// 精确匹配
			String requestid = "53453gge4er3freg";

			sellerBean.setModule("seller");
			sellerBean.setAppkey(1);
			sellerBean.setQuery("term(" + originName + "," + keyword + ")");
			sellerBean.setPlatform("pc");
			sellerBean.setExplain("Flase");
			sellerBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,sort:apphaohuo1");
			sellerBean.setFl("iuserid,shopname,nickname,originnickname,ismerchant");
			sellerBean.setLimit("0,10");
			sellerBean.setRequestid(requestid);

			sellerCall.setData(sellerBean);
			sellerCall.callService();

			JSONArray hits = sellerCall.getHits();
			
			if(hits.toString().equals("[]")){
				Logger.verifyEquals(false,hits.toString().equals("[]") , "没有精确匹配到店铺信息");
			}else{
				JSONObject hitOne = new JSONObject(hits.get(0).toString());
				
				Logger.verifyEquals(1, sellerCall.getTotal(), "检查返回total值是否为1");
				Logger.verifyEquals(1, hits.length(), "精确匹配只会返回1条店铺信息记录");
				Logger.verifyEquals(keyword, hitOne.get("originnickname"), "检查完整匹配商户名");
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Seller_005() {
		Logger.start(true,"店铺搜索- 验证接口返回的公共数据");
		try {
			//设置参数
			String keyword = "奶粉";
			String vagueName = "nickname"; //模糊匹配
			String originName = "originnickname";//精确匹配
			String requestid = "53453gge4er3freg";
			
			sellerBean.setModule("seller");
			sellerBean.setAppkey(1);
			sellerBean.setQuery("keyword("+vagueName+","+keyword+")");
			sellerBean.setPlatform("pc");
			sellerBean.setExplain("Flase");
			sellerBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,sort:apphaohuo1");
			sellerBean.setFl("iuserid,ismerchant");
			sellerBean.setLimit("0,10");
			sellerBean.setRequestid(requestid);
			
			sellerCall.setData(sellerBean);
			sellerCall.callService();	
			
			Logger.verifyEquals("OK",sellerCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(sellerCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, sellerCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(requestid,sellerCall.getCommonInfo("requestid") , "检查接口请求id值");
			Logger.verifyEquals(false, sellerCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			Logger.verifyNotNull(sellerCall.getCommonInfo("hostip"), "验证hostip不为空");
				
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Seller_006() {
		Logger.start(true,"店铺搜索- 验证返回的店铺类型与数据库中是否一致");
		try {
			//设置参数
			//String keyword = "奶粉";
			String vagueName = "nickname"; //模糊匹配
			String originName = "originnickname";//精确匹配
			String requestid = "53453gge4er3freg";
			
			sellerBean.setModule("seller");
			sellerBean.setAppkey(1);
			//sellerBean.setQuery("keyword("+vagueName+","+keyword+")");
			sellerBean.setPlatform("pc");
			sellerBean.setExplain("Flase");
			sellerBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,sort:apphaohuo1");
			sellerBean.setFl("iuserid,ismerchant");
			//sellerBean.setLimit("0,100");
			sellerBean.setRequestid(requestid);
			
			sellerCall.setData(sellerBean);
			sellerCall.callService();	
			
			
			for (int i =1 ;i<=sellerCall.getHits().length();i++){
				
				String info = sellerCall.getHits().get(i - 1).toString();
				JSONObject infoAry = new JSONObject(info);
				
				int ismerchant = (Integer) infoAry.get("ismerchant");
				int uid =  (Integer) infoAry.get("iuserid");
				
				//检查数据库中该店主的类型
				List<Map> usertypeInfo = new SearchWapper().selectSellerType(uid);
				int mshop = (Integer) (usertypeInfo.get(0).get("mshop")==null ? 0 : usertypeInfo.get(0).get("mshop"));
				
				Logger.verifyEquals(mshop, ismerchant, "验证第"+i+"条记录中店主id为"+uid+"的类型是否为"+"ismerchant");
					
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Seller_007() {
		Logger.start(false,"店铺搜索- 传入不同的返回参数值,检查返回店铺信息中是否包含该参数值");
		try {
			//设置参数
			String keyword = "奶粉";
			String vagueName = "nickname"; //模糊匹配
			String requestid = "53453gge4er3freg";
			
			sellerBean.setModule("seller");
			sellerBean.setAppkey(1);
			sellerBean.setQuery("keyword("+vagueName+","+keyword+")");
			sellerBean.setPlatform("pc");
			sellerBean.setExplain("Flase");
			sellerBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,sort:apphaohuo1");
			sellerBean.setFl("iuserid,shopname,nickname,originnickname,ismerchant");
			sellerBean.setLimit("0,10");
			sellerBean.setRequestid(requestid);
			
			sellerCall.setData(sellerBean);
			sellerCall.callService();	
			
			JSONArray hitsAry = sellerCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 
			 //获取接口返回的key校验返回字段
			  Logger.verifyEquals(true,infoAry.has("iuserid") , "验证第"+i+"条记录返回结果中有iuserid这个key");
			  Logger.verifyEquals(true, infoAry.has("ismerchant"), "验证第"+i+"条记录返回结果中有ismerchant这个key");
			  Logger.verifyEquals(true,infoAry.has("nickname") , "验证第"+i+"条记录返回结果中有nickname这个key");
			  Logger.verifyEquals(true,infoAry.has("shopname") , "验证第"+i+"条记录返回结果中有shopname这个key");
			  Logger.verifyEquals(true,infoAry.has("originnickname") , "验证第"+i+"条记录返回结果中有originnickname这个key");
			  
			}
		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Seller_008() {
		Logger.start(false,"店铺搜索- 检索无任何匹配结果的店铺名，检查返回 数据");
		try {
			//设置参数
			String keyword = "543trretretrgfg";
			String vagueName = "nickname"; //模糊匹配
			String requestid = "53453gge4er3freg";
			
			sellerBean.setModule("seller");
			sellerBean.setAppkey(1);
			sellerBean.setQuery("keyword("+vagueName+","+keyword+")");
			sellerBean.setPlatform("pc");
			sellerBean.setExplain("Flase");
			sellerBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,sort:apphaohuo1");
			sellerBean.setFl("iuserid,shopname,nickname,originnickname,ismerchant");
			sellerBean.setLimit("0,10");
			sellerBean.setRequestid(requestid);
			
			sellerCall.setData(sellerBean);
			sellerCall.callService();	
			
			Logger.verifyEquals(true, sellerCall.getHits().toString().equals("[]"), "返回店铺信息为空");
			Logger.verifyEquals("OK", sellerCall.getStatus(), "接口返回状态值正常");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Seller_009() {
		Logger.start(false,"店铺搜索- 关键字为空，检查返回数据总数与数据库中查的结果是否一致");
		try {
			//设置参数
			String requestid = "53453gge4er3freg";
			
			sellerBean.setModule("seller");
			sellerBean.setAppkey(1);
			sellerBean.setPlatform("pc");
			sellerBean.setExplain("Flase");
			sellerBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,sort:apphaohuo1");
			sellerBean.setFl("iuserid,shopname,nickname,originnickname,ismerchant");
			sellerBean.setLimit("0,10");
			sellerBean.setRequestid(requestid);
			
			sellerCall.setData(sellerBean);
			sellerCall.callService();	
			
			
			//连接数据库查询不在惩罚期间的所有店铺
			int sellerNum = new SearchWapper().selectSellerNum();
			
			Logger.verifyEquals(sellerNum, sellerCall.getTotal(), "比较数据库中统计出的total结果和接口返回的total结果");
			
			Logger.verifyEquals(false, sellerCall.getHits().toString().equals("[]"), "返回所有店铺信息");
			Logger.verifyEquals("OK", sellerCall.getStatus(), "接口返回状态值正常");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Seller_010() {
		Logger.start(false,"店铺搜索- 传入不存在的返回值参数，检查返回店铺信息是否包含不存在返回值");
		try {
			//设置参数
			String keyword = "奶粉";
			String vagueName = "nickname"; //模糊匹配
			String requestid = "53453gge4er3freg";
			
			sellerBean.setModule("seller");
			sellerBean.setAppkey(1);
			sellerBean.setQuery("keyword("+vagueName+","+keyword+")");
			sellerBean.setPlatform("pc");
			sellerBean.setExplain("Flase");
			sellerBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,sort:apphaohuo1");
			sellerBean.setFl("uid,iuserid,ismerchant,nickname,unames");//uid和unames是不存在的字段值
			sellerBean.setLimit("0,10");
			sellerBean.setRequestid(requestid);
			
			sellerCall.setData(sellerBean);
			sellerCall.callService();	
			
			JSONArray hitsAry = sellerCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 
			 //获取接口返回的key校验返回字段
			  Logger.verifyEquals(false, infoAry.has("uid"), "验证第"+i+"条记录返回结果无uid这个key");
			  Logger.verifyEquals(false, infoAry.has("unames"), "验证第"+i+"条记录返回结果无unames这个key");
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Seller_011() {
		Logger.start(true,"店铺搜索- 验证分页功能");
		try {
			//设置参数
			String keyword = "奶粉";
			String vagueName = "nickname"; //模糊匹配
			String requestid = "53453gge4er3freg";
			int number =15;
			
			sellerBean.setModule("seller");
			sellerBean.setAppkey(1);
			sellerBean.setQuery("keyword("+vagueName+","+keyword+")");
			sellerBean.setPlatform("pc");
			sellerBean.setExplain("Flase");
			sellerBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,sort:apphaohuo1");
			sellerBean.setFl("iuserid,shopname,nickname,originnickname,ismerchant");
			sellerBean.setLimit("0,"+number+"");//设置每页显示15条记录
			sellerBean.setRequestid(requestid);
			
			sellerCall.setData(sellerBean);
			sellerCall.callService();	
			
			//如果返回总记录数大于每页显示数则每页显示所传值，否则每天显示总数值
			int exNume = number < (int)sellerCall.getHits().length() ? number : (int)sellerCall.getHits().length();
			
			Logger.verifyEquals(exNume, (int)sellerCall.getHits().length(), "每页显示"+number+"条数据信息");
			Logger.verifyEquals("OK", sellerCall.getStatus(), "接口返回状态值正常");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_Seller_012() {
		Logger.start(false,"店铺搜索- 通过比较数据库中的惩罚表，验证接口返回匹配店铺是否处于受惩罚期间，不会返回店铺信息");
		try {
			//设置参数
			//String keyword = "奶粉";
			String vagueName = "nickname"; //模糊匹配
			String requestid = "53453gge4er3freg";

			sellerBean.setModule("seller");
			sellerBean.setAppkey(1);
		//	sellerBean.setQuery("keyword("+vagueName+","+keyword+")");
			sellerBean.setPlatform("pc");
			sellerBean.setExplain("Flase");
			sellerBean.setInfo("userid:111,cookieid:111555555555,idfa:idfavalue,imei:imeivalue,sort:apphaohuo1");
			sellerBean.setFl("iuserid,shopname,nickname,originnickname,ismerchant");
			//sellerBean.setLimit("0,100");
			sellerBean.setRequestid(requestid);

			sellerCall.setData(sellerBean);
			sellerCall.callService();

			JSONArray hits = sellerCall.getHits();
			
			//查找出店主“比邻优品” 的惩罚结束时间
			//连接数据库查看惩罚信息
			
			for (int i = 1; i <= hits.length(); i++) {
				String hitsOne = hits.getString(i - 1);
				JSONObject hitsOnes = new JSONObject(hitsOne);
				int uid = (Integer) hitsOnes.get("iuserid");
				
				//检查数据库该店主是否存在惩罚表中
				List<Map> userpuchInf = new SearchWapper().selectUserPunishInfo(uid);
				
				if(userpuchInf.size()<1){
					//店主不在惩罚表
					Logger.verifyEquals(0, (int)userpuchInf.size(), "第"+i+"条店铺信息不在惩罚表，应该正常显示");
					
				}else{
					Date maxPunishEndTime = (Date) userpuchInf.get(0).get("maxPunishEndTime");
					long nowTime = new Date().getTime();
					long punishTime = maxPunishEndTime.getTime();
					Logger.verifyEquals(true, punishTime > nowTime, "第"+i+"条记录店铺信息惩罚还未结束，不应该显示");
				}
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}
